﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using LinkCard.Dao;
using LinkCard.Model;
using LinkCard.Utils;

namespace LinkCard.ShopCP.User
{
    public partial class user_new : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            else
            {
                if (Convert.ToBoolean(Session["SHOPADMIN"]) == false)
                {
                    Response.Redirect("/message_box.aspx?msg=5");
                }
            }
        }
        [System.Web.Services.WebMethod]
        public static bool CheckUserName(string sUsername)
        {
            string sSQL = "SELECT Username FROM ShopStaff WHERE Username = N'" + sUsername + "'";
            DataTable dt = DataHelper.SelectTable(sSQL, false);
            if (dt.Rows.Count > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                var dao = new ShopStaffDao();
                var _user = new Model.ShopStaff();

                _user.Username = Username.Value;
                _user.Password = Password.Value;
                //_user.Permission = sPermission;
                _user.Description = Description.Value;
                _user.Fullname = Fullname.Value;
                _user.MobilePhone = MobilePhone.Value;
                _user.Address = Address.Value;
                _user.Email = Email.Value;
                _user.Type = "NORMAL";
                _user.SupplierID = Convert.ToInt64(Session["USER"].ToString());
                
                dao.Add(_user);

                //Lưu log
                var logAction = LogActions.LogItem(0,Convert.ToInt64(Session["USER"].ToString()), 0, 0, _user.UserID, 
                                                    "Thêm mới Đại lý " + Username.Value,
                                                    "New", "ShopCP");
              
                Response.Redirect("/User/user_list.aspx", false);

            }catch(Exception ex){
                Response.Redirect("error_info.aspx?err=addstaff");
            }

        }
    }
}